<template>
  <div class="Registerbody">
    <div class="Registerdata">
      <div class="Registertext">
        <h2>注册用户</h2>
      </div>
      <div class="formdata">
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              clearable
              placeholder="请输入账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              clearable
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item prop="repassword">
            <el-input
              v-model="form.repassword"
              clearable
              placeholder="请再次输入密码"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="tool">
        <!-- <div>
          <el-checkbox v-model="checked" @change="remenber"
            >记住密码</el-checkbox
          >
        </div> -->
      </div>
      <div class="butt">
        <el-button type="primary" @click.native.prevent="Register('form')"
          >注册</el-button
        >
        <el-button class="shou" @click="clean">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { Register } from "@/api/userApi";

export default {
  name: "indexRegister",
  data() {
    return {
      form: {
        password: "",
        username: "",
        repassword: "",
      },
      checked: false,
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { max: 20, message: "不能大于20个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    // if(localStorage.getItem("news")){
    //   this.form=JSON.parse(localStorage.getItem("news"))
    //   this.checked=true
    // }
  },
  methods: {
    clean() {
      console.log("清空");
      (this.form.password = ""),
        (this.form.username = ""),
        (this.form.repassword = "");
    },

    Register(form) {
      this.$refs[form].validate((valid) => {
        console.log(valid);
        if (valid) {
          if (this.form.repassword == this.form.password) {
            var data = {
              username: this.form.username,
              password: this.form.password,
            };
            Register(data)
              .then((res) => {
                console.log(res);
                if (res.code == 1000) {
                  this.$message({
                    message: "注册成功啦",
                    type: "success",
                    showClose: true,
                  });

                  //跳转到登录页面
                  this.$router.push("/login");
                } else {
                  this.$message({
                    message: "账户名或密码错误",
                    type: "error",
                    showClose: true,
                  });
                }
              })
              .catch((err) => {
                console.log(err);
                this.$message({
                  message: "账户名或密码错误",
                  type: "error",
                  showClose: true,
                });
              });
          } else {
            this.$message({
              message: "两次输入的密码不一致！",
              type: "error",
              showClose: true,
            });
          }
          //判断是否有值
        }
      });
    },
  },
};
</script>

<style scoped>
.Registerbody {
  width: 100%;
  height: 100%;
  min-width: 1000px;
  background-image: url("@/assets/background.jpg");
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  background-repeat: no-repeat;
  position: fixed;
  line-height: 100%;
  padding-top: 150px;
}

.Registertext {
  margin-bottom: 20px;
  line-height: 50px;
  text-align: center;
  font-size: 30px;
  font-weight: bolder;
  color: white;
  text-shadow: 2px 2px 4px #000000;
}

.Registerdata {
  width: 400px;
  height: 300px;
  transform: translate(-50%);
  margin-left: 50%;
}

.tool {
  display: flex;
  justify-content: space-between;
  color: #606266;
}

.butt {
  margin-top: 10px;
  text-align: center;
}

.shou {
  cursor: pointer;
  color: #606266;
}

/*ui*/
/* /deep/ .el-form-item__label {
  font-weight: bolder;
  font-size: 15px;
  text-align: left;
}

/deep/ .el-button {
  width: 100%;
  margin-bottom: 10px;

} */
</style>

